<?php

namespace app\controllers\admin\uni\system;

use app\controllers\admin\base\AdminController;
use app\models\system\SetExpressCity as Model;
use app\models\City;

class ExpressAreaController extends AdminController
{
    public function actionFetch()
    {
        $path = __DIR__ . '/../../cache/province.json';

        if (!file_exists($path)) {
            $province = City::getProvince();
            $province = json_encode($province);
            file_put_contents($path, $province);
        }

        $data = file_get_contents($path);
        $filesize = filesize($path);

        // 设置(勾选)的city
        $express = Model::getLastOne();

        return $this->success([
            'province' => $data,
            'express' => $express,
            'Bytes' => $filesize,
            'KB' => round($filesize / 1024, 2),
        ]);
    }

    public function actionSave()
    {
        $selected = $this->post('selected');

        if (Model::saveData($selected)) {
            return $this->success();
        }

        return $this->fail();
    }

}